package leetcode.templates.arrays;

import java.util.ArrayList;
import java.util.List;

public class MaxArrays {
	
	public List<Integer>[] ways(int m, int n) {
		int len = m * n;
		List<Integer>[] ways = new ArrayList[len];
		for (int i = 0; i < len; i++) {
			ways[i] = new ArrayList<>();
		}
		for (int x = 0; x < m; x++) {
			for (int y = 0; y < n; y++) {
				int index = index(x, y, n);
				for (int i = 0; i < 8; i += 2) {
					int nextx = x + d[i], nexty = y + d[i + 1];
					if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) {
						int nextIndex = index(nextx, nexty, n);
						ways[index].add(nextIndex);
					}
				}
			}
		}
		return ways;
	}

	// 二维坐标转一维坐标
	public int index(int i, int j, int n) {
		return i * n + j;
	}

	// 四方遍历数组，{左，右，上，下}
	int[] d = new int[] { 0, -1, 0, 1, -1, 0, 1, 0 };

	// 四方遍历template
	public void iteration4(int x, int y, int m, int n) {
		for (int i = 0; i < 8; i += 2) {
			int nextx = x + d[i], nexty = y + d[i + 1];
			int nextIndex = this.index(nextx, nexty, n);
			if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) {

			}
		}
	}

	// 八方遍历数组，{左上，上，右上，左，中，右，左下，下，右下}
	int[] direction = new int[] { -1, -1, -1, 0, -1, 1, 0, -1, 0, 0, 0, 1, 1, -1, 1, 0, 1, 1 };

	public int[][] iteration9(int[][] array) {
		int m = array.length, n = array[0].length;
		int[][] ans = new int[m][n];
		int[] direction = new int[] { -1, -1, -1, 0, -1, 1, 0, -1, 0, 0, 0, 1, 1, -1, 1, 0, 1, 1 };
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
//				int sum = 0, num = 0;
				for (int k = 0; k < direction.length; k += 2) {
					int x = i + direction[k], y = j + direction[k + 1];
					if (x >= 0 && x < m && y >= 0 && y < n) {
//						sum += array[x][y];
//						num += 1;
					}
				}
//				ans[i][j] = sum / num;
			}
		}
		return ans;
	}

}
